package com.jt.sp02.controller;

import com.jt.sp01.entity.Item;
import com.jt.sp01.service.ItemService;

import com.jt.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 根据订单id获取订单列表
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable(name = "orderId")String orderId ){
        List<Item> items = itemService.getItems(orderId);

        JsonResult<List<Item>> ok = JsonResult.ok(items);
        return ok;
    }

    /**
     * 删除订单列表
     * @param items
     * @return
     */
    @PostMapping("/decreaseNumber")
public JsonResult<?> decreaseNumber(@RequestBody List<Item> items) throws InterruptedException {
        itemService.decreaseNumber(items);
        /**
         * 一半的几率线程阻塞 会报500异常
         */
        if (Math.random()<0.5){
            log.info("当前Thread 阻塞了");
            TimeUnit.SECONDS.sleep(50);

        }
        return JsonResult.ok().msg("减少库存车");
}
    @GetMapping("/favicon.ico")
    public void favicon(){}
}
